package jmh;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;


@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS)
public class TestStream {


    @Benchmark
    public void testStringAdd() {
        LongStream intStream = LongStream.range(1, 100000000);
        Stream<Long> integerStream = intStream.boxed();
        System.out.println(integerStream.reduce(0L, (a, b) -> a + b));
    }

    @Benchmark
    public void testStringBuilderAdd() {
        LongStream intStream = LongStream.range(1, 100000000);
        Stream<Long> integerStream = intStream.boxed();
        System.out.println(integerStream.parallel().reduce(0L, (a, b) -> a + b));
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
            .include(TestStream.class.getSimpleName())
            .forks(1)
            .build();

        new Runner(opt).run();
    }
}
